﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Domain.Models;
using IServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ViewModel.CompanyModel;
using ViewModel.Response;

namespace Management.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CompanyController : ControllerBase
    {
        private readonly ICompanyService companyService;

        public CompanyController(ICompanyService companyService)
        {
            this.companyService = companyService;
        }
        /// <summary>
        /// 获取所有的公司
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResponseModel<IEnumerable<Company>> Get()
        {
            var result = new ResponseModel<IEnumerable<Company>>
            {
                Data = companyService.GetCompanies(),
                Status = ResponseStatus.OK
            };
            return result;
        }
        /// <summary>
        /// 分页获取公司
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <returns></returns>
        [HttpGet("page")]
        public ResponseModel<PageModel<IEnumerable<Company>>> GetPaged(int pageIndex = 1)
        {
            var pageSize = 10;
            var result = new ResponseModel<PageModel<IEnumerable<Company>>>
            {
                Data = companyService.CreatePage(companyService.GetCompanies(),pageSize,pageIndex),
                Status = ResponseStatus.OK
            };
            return result;
        }
        /// <summary>
        /// 获取公司
        /// </summary>
        /// <param name="id">公司编号</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public ResponseModel<Company> GetCompany(int id)
        {
            var result = new ResponseModel<Company>
            {
                Data = companyService.GetCompany(id),
                Status = ResponseStatus.OK
            };
            return result;
        }
        /// <summary>
        /// 创建公司
        /// </summary>
        /// <param name="model">公司实体</param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel<string> AddCompany([FromBody] CreateCompanyModel model)
        {
            var result = new ResponseModel<string>();
            var createResult = this.companyService.CreateCompany(model);
            if (createResult > 0)
            {
                result.Data = createResult.ToString();
                result.Status = ResponseStatus.OK;
            }
            return result;
        }

        /// <summary>
        /// 删除公司
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public ResponseModel<string> Remove(int id)
        {
            var result = new ResponseModel<string>();
            var entity = this.companyService.QueryById(id);
            if (entity == null)
            {
                result.Data = false.ToString();
                result.Status = ResponseStatus.NotFound;
                return result;
            }
            if (this.companyService.DeleteById(id))
            {
                result.Data = true.ToString();
                result.Status = ResponseStatus.OK;
            }
            return result;
        }

    }
}